begintownscript;

variables;
short bmessage,rctx,rcty,talk,leadchar;
string custom_m;

body;

beginstate INIT_STATE;
	turn_off_training(1);

	if(get_flag(73,0) == 0){
		force_instant_terrain_redraw();
		message_dialog("...wow.  The pit here really isn't particularly well-defended.  In fact, to hell with that-- it isn't defended at all.  Not that they'd have to-- it's just a hole in the ground.  Still, don't look a gift horse in the mouth.","There are two odd-looking devices flanking the entryway, but they appear to be dormant.");
		set_flag(73,0,1);
	}
	if(get_flag(74,0) == 3){
		set_terrain(16,35,385);
		set_terrain(32,35,385);
	}
	if(get_flag(76,0) == 1){
		set_terrain(17,33,375);
		set_terrain(31,33,374);
		set_terrain(18,33,440);
		set_terrain(30,33,440);
	}
	if(get_flag(76,0) == 0){
		set_terrain(17,33,374);
		set_terrain(31,33,375);
		set_terrain(18,33,441);
		set_terrain(30,33,441);
	}

break;

beginstate EXIT_STATE;
break;

beginstate START_STATE;
break;

beginstate 10;

	if(get_flag(74,0) == 0){
		leadchar = 0;
		while(char_ok(leadchar) == FALSE){
			leadchar = (leadchar + 1);
		}
		if(char_loc_x(leadchar) >= 33)
			put_straight_zap(34,35,40,35,0);
		if(char_loc_x(leadchar) <= 15)
			put_straight_zap(14,35,12,35,0);
		if((char_loc_x(leadchar) >= 17) && (char_loc_x(leadchar) <= 31))
			put_straight_zap(18,35,30,35,0);
		run_animation_sound(51);
		damage_char(leadchar,get_ran(2,25,100),1);

		message_dialog("Ow!  So that's what the machines are there for.  But how are they to be passed?","");

		set_flag(74,0,1);
	}
	if((get_flag(74,0) == 1) || (get_flag(74,0) == 2)){
		reset_dialog();
		add_dialog_str(0,"Hmm...",0);
		add_dialog_choice(0,"Can't do anything for now.");
		add_dialog_choice(1,"Try running through again.");
		if((get_highest_skill(2) > (2 + get_ran(1,0,16)) || (get_flag(74,0) == 2)))
			add_dialog_choice(2,"Try holding the emblem in front of the beam.");
		bmessage = run_dialog(0);
		if(bmessage == 2){
			leadchar = 0;
			while(char_ok(leadchar) == FALSE){
				leadchar = (leadchar + 1);
			}
			if(char_loc_x(leadchar) >= 33)
				put_straight_zap(34,35,40,35,0);
			if(char_loc_x(leadchar) <= 15)
				put_straight_zap(14,35,12,35,0);
			if((char_loc_x(leadchar) >= 17) && (char_loc_x(leadchar) <= 31))
				put_straight_zap(18,35,30,35,0);
			run_animation_sound(51);
			damage_char(leadchar,get_ran(2,25,100),1);
			message_dialog("Nope.  That still doesn't work.","");
		}
		if(bmessage == 3){
			message_dialog("Yes!  That seems to have done the trick.  The beam is deactivated...  for now.  Time to cross...","");
			leadchar = 0;
			while(char_ok(leadchar) == FALSE){
				leadchar = (leadchar + 1);
			}
			if(char_loc_y(leadchar) >= 36){
				rctx = (char_loc_x(leadchar));
				march_party(rctx,35);
				force_view_center(rctx,35);
				force_instant_terrain_redraw();
				play_sound(49);
				pause(2);

				march_party(rctx,34);
				force_view_center(rctx,34);
				force_instant_terrain_redraw();
				play_sound(49);
				pause(2);

				if(party_size() >= 2){
					if((rctx == 28) || (rctx == 20)){
			march_party((rctx - 1),33);
			force_view_center((rctx - 1),33);
					}
					else{
						march_party(rctx,33);
						force_view_center(rctx,33);
					}
					force_instant_terrain_redraw();
					play_sound(50);
					pause(2);
				}

				if(party_size() >= 3){
					march_party(rctx,32);
					force_view_center(rctx,32);
					force_instant_terrain_redraw();
					play_sound(49);
					pause(2);
				}

				if(party_size() == 4){
					if((rctx >= 22) && (rctx <= 26)){
			march_party((rctx - 1),32);
			force_view_center((rctx - 1),32);
					}
					else{
						march_party(rctx,31);
						force_view_center(rctx,31);
					}
					force_instant_terrain_redraw();
					play_sound(50);
					pause(2);
				}
			}
			else{
				rctx = (char_loc_x(leadchar));
				march_party(rctx,35);
				force_view_center(rctx,35);
				force_instant_terrain_redraw();
				play_sound(49);
				pause(2);

				march_party(rctx,36);
				force_view_center(rctx,36);
				force_instant_terrain_redraw();
				play_sound(49);
				pause(2);

				if(party_size() >= 2){
					march_party(rctx,37);
					force_view_center(rctx,37);
					force_instant_terrain_redraw();
					play_sound(50);
					pause(2);
				}

				if(party_size() >= 3){
					march_party(rctx,38);
					force_view_center(rctx,38);
					force_instant_terrain_redraw();
					play_sound(49);
					pause(2);
				}

				if(party_size() == 4){
					march_party(rctx,39);
					force_view_center(rctx,39);
					force_instant_terrain_redraw();
					play_sound(50);
					pause(2);
				}
			}
			if(get_flag(74,0) == 1)
				set_flag(74,0,2);
		}
		block_entry(1);
	}
	if(get_flag(74,0) == 3){
		message_dialog("No.  These beams are set to go off, no matter WHAT a person has.  It's no longer safe.","");
		block_entry(1);
	}

break;

beginstate 11;

	if(is_combat() == TRUE)
		end();
	reset_dialog();
	if(party_size() == 1)
		add_dialog_str(0,"The pit to the brigands...  It's here, if I'm willing to take the leap, though I won't be able to come back.",0);
	if(party_size() > 1)
		add_dialog_str(0,"The pit to the brigands...  It's here, if we're willing to take the leap, though we won't be able to come back.",0);
	add_dialog_choice(0,"Jump");
	add_dialog_choice(1,"Wait");
	bmessage = run_dialog(0);
	if(bmessage == 2)
		end();
	march_party(1,1);
	march_party(1,1);
	march_party(1,1);
	march_party(1,1);
	set_total_visibility(1);
	force_view_center(24,24);
	force_instant_terrain_redraw();
	play_sound(100);
	pause(5);

	rcty = 25;
	while(rcty <= 34){
		force_view_center(24,rcty);
		force_instant_terrain_redraw();
		pause(1);

		rcty = (rcty + 1);
	}
	pause(3);

	activate_hidden_group(1);
	put_boom_on_space(24,34,2,0);
	force_instant_terrain_redraw();
	run_animation_sound(10);
	pause(3);

	text_bubble_on_char(6,"...");
	force_instant_terrain_redraw();
	pause(16);

	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"Fine.");
	force_instant_terrain_redraw();
	pause(14);

	text_bubble_on_char(6,"");
	text_bubble_on_char(6,"Let's rumble.");
	force_instant_terrain_redraw();
	pause(16);

	text_bubble_on_char(6,"");
	set_character_facing(6,2);
	force_instant_terrain_redraw();
	play_sound(18);
	pause(2);

	text_bubble_on_char(6,"Yaaaaah!");
	rcty = 33;
	while(rcty >= 23){
		relocate_character(6,24,rcty);
		force_view_center(24,rcty);
		force_instant_terrain_redraw();
		pause(1);

		rcty = (rcty - 1);
	}
	text_bubble_on_char(6,"");
	erase_char(6);
	force_instant_terrain_redraw();
	set_total_visibility(0);

	move_to_new_town(15,16,14);

break;

beginstate 30;

	if((get_flag(74,0) < 3) && (get_flag(74,0) > 0)){
		reset_dialog();
		add_dialog_str(0,"Hmm...  It must have something to be done with these devices.",0);
		add_dialog_choice(0,"Leave it be.");
		add_dialog_choice(1,"Break it.");
		add_dialog_choice(2,"Try to fix it.");
		bmessage = run_dialog(0);

		if(bmessage == 1)
			end();
		if(bmessage == 2){
			if(get_highest_skill(0) >= 10){
				leadchar = 0;
				while(char_ok(leadchar) == FALSE){
					leadchar = (leadchar + 1);
				}
				if(char_loc_x(leadchar) < 24){
					set_terrain(16,35,385);
					put_boom_on_space(16,35,1,0);
					force_instant_terrain_redraw();
					run_animation_sound(5);
					pause(5);

					force_view_center(28,34);
					set_total_visibility(1);
					set_terrain(32,35,385);
					put_boom_on_space(32,35,1,0);
					force_instant_terrain_redraw();
					run_animation_sound(5);
					pause(5);

					leadchar = 0;
					while(char_ok(leadchar) == FALSE){
						leadchar = (leadchar + 1);
					}
					rctx = (char_loc_x(leadchar));
					rcty = (char_loc_y(leadchar));
					force_view_center(rctx,rcty);
					force_instant_terrain_redraw();
				}
				else{
					set_terrain(32,35,385);
					put_boom_on_space(32,35,1,0);
					force_instant_terrain_redraw();
					run_animation_sound(5);
					pause(5);

					force_view_center(20,34);
					set_total_visibility(1);
					set_terrain(16,35,385);
					put_boom_on_space(16,35,1,0);
					force_instant_terrain_redraw();
					run_animation_sound(5);
					pause(5);

					leadchar = 0;
					while(char_ok(leadchar) == FALSE){
						leadchar = (leadchar + 1);
					}
					rctx = (char_loc_x(leadchar));
					rcty = (char_loc_y(leadchar));
					force_view_center(rctx,rcty);
					force_instant_terrain_redraw();
				}
				set_total_visibility(0);
				force_instant_terrain_redraw();

				message_dialog("Wow.  As one of the devices is smashed and releases intense amounts of power, the other one shorts out and also lets out a large amount of energy.  Now, these beams should DEFINITELY be non-functional.","");
				set_flag(74,0,4);
			}
			else{
				message_dialog("Nope...  Not strong enough.  This machine is fairly resilient.","");
			}
		}
		if(bmessage == 3){
			if(get_highest_skill(2) >= 12){
				reset_dialog();
				add_dialog_str(0,"Hmm...  The machinery is pretty complex, but manageable.  It could also be set to sear anyone trying to get by, which would prevent any brigands from getting back down to their base.",0);
				add_dialog_choice(0,"Do that!");
				add_dialog_choice(1,"No, disabling them should be sufficient.");
				bmessage = run_dialog(1);
				if(bmessage == 2){
					set_flag(74,0,5);
					message_dialog("Yeah...  That should do it.  Not particularly difficult, just a few wires that need to be sliced off here and there, some runes to be scratched of...  The usual.","");
				}
				if(bmessage == 1){
					leadchar = 0;
					while(char_ok(leadchar) == FALSE){
						leadchar = (leadchar + 1);
					}
					if(char_loc_y(leadchar) > 35){
						message_dialog("No...  It'd be best to get on the right side of the beam before permanently setting them to sear everyone trying to get through.","");
						end();
					}
					else{
						set_flag(74,0,3);
						message_dialog("There.  It's done.  Now, the brigands won't be able to reinforce themselves.  Nobody will be getting by here any time soon.","");
					}
				}
			}
			else{
				if(get_highest_skill(2) >= 6){
					set_flag(74,0,5);
					message_dialog("Yeah...  That should do it.  Not particularly difficult, just a few wires that need to be sliced off here and there, some runes to be scratched of...  The usual.","");
				}
				else{
					message_dialog("Hm...  This machine is actually pretty complex and difficult to disarm.","In other words, no dice.");
				}
			}
		}
	}

break;

beginstate 31;

	if(get_flag(76,0) == 0)
		message_dialog("These strange, glass tubes have to have some purpose.  The green tube is lit by some magical force and the red one is not...  Hopefully, that means _there is nothing dangerous here_ and nothing more.","");
	else
		message_dialog("These strange, glass tubes have to have some purpose.  The red tube is lit by some magical force and the green one is not...  Hopefully, that means _there is nothing dangerous here_ and nothing more.","");

break;

beginstate 32;

	reset_dialog_preset_options(2);
	bmessage = run_dialog(0);
	if(bmessage == 1)
		end();
	play_sound(99);
	if(get_flag(76,0) == 0){
		set_terrain(17,33,375);
		set_terrain(31,33,374);
		set_terrain(18,33,440);
		set_terrain(30,33,440);
		set_flag(76,0,1);
	}
	else{
		set_terrain(17,33,374);
		set_terrain(31,33,375);
		set_terrain(18,33,441);
		set_terrain(30,33,441);
		set_flag(76,0,0);
	}
	force_instant_terrain_redraw();
	play_sound(94);

break;

